﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmLogin : Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 登录系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            //获取用户输入信息
            string UserName = txtUserName.Text.Trim();
            string Password = txtPassword.Text.Trim();
            //判断是否为空
            if (string.IsNullOrEmpty(UserName))
            {
                MessageBox.Show("请输入账号！","登录提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
                txtUserName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(Password))
            {
                MessageBox.Show("请输入密码！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Focus();
                return;
            }

            //与数据库通信 检查输入与数据库中是否一致
            {
                #region 未进行封装
                //建立与数据库的连接
                //连接字符串--钥匙
                //string connString = "server=DESKTOP-O280DGB;database=Test;integrated Security=true";//字符串  Windows身份验证

                //string connString = "server=DESKTOP-O280DGB;database=Test;uid=admin;pwd=admin";//Sql Server身份验证  "Data Source;Initial Catalog;User Id;Password"
                //SqlConnection conn = new SqlConnection(connString);

                #region 方法一                 
                ////写查询语句 拼接式 Sql注入
                //string sql = "select count(*) from Student where Sname='" + UserName + "' and Sno='" + Password + "'";
                ////创建Command对象
                //SqlCommand cmd = new SqlCommand(sql, conn);
                #endregion

                #region 方法二
                ////写查询语句 使用参数化Sql
                //string sql = "select count(*) from Student where Sname=@Sname and Sno=@Sno";
                ////添加参数
                //SqlParameter paraSname = new SqlParameter("@Sname", UserName);
                //SqlParameter paraSno = new SqlParameter("@Sno", Password);
                ////创建Command对象
                //SqlCommand cmd = new SqlCommand(sql, conn);
                //cmd.Parameters.Clear();
                //cmd.Parameters.Add(paraSname);
                //cmd.Parameters.Add(paraSno);
                #endregion

                #region 方法三
                ////写查询语句 使用参数化Sql 数组 
                //string sql = "select count(*) from Student where Sname=@Sname and Sno=@Sno";
                ////添加参数
                //SqlParameter[] paras =
                //{
                //    new SqlParameter("@Sname",UserName),
                //    new SqlParameter("@Sno",Password)
                //};
                ////创建Command对象
                //SqlCommand cmd = new SqlCommand(sql, conn);
                //cmd.Parameters.Clear();
                //cmd.Parameters.AddRange(paras);
                #endregion

                ////打开连接
                //conn.Open();//最晚打开 最早关闭//执行命令 要求必须在连接状态 Opened
                //object o = cmd.ExecuteScalar();//执行查询，返回结果集第一行第一列的值忽略其他行或列
                //conn.Close();//关闭连接
                #endregion

                //优化
                string sql = "select count(*) from Student where Sname=@Sname and Sno=@Sno";
                SqlParameter[] paras =
                {
                    new SqlParameter("@Sname",UserName),
                    new SqlParameter("@Sno",Password)
                };
                object o = SqlHelper.ExecuteScalar(sql, paras);

                //处理结果
                if (o==null||(o==DBNull.Value)||((int)o==0))
                {
                    MessageBox.Show("登录账号或密码错误！", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    MessageBox.Show("登录成功！","登录提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    //转到主页面
                    FrmMain frmMain = new FrmMain();
                    frmMain.Show();
                    this.Hide();
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
            //Application.Exit();
        }
    }
}
